import os
import sys
import re


def handler1(string):
    '''
    处理带括号的负数
    '''    
    r = re.search(r'\(-[.0-9]+(e-[0-9]+)?\)', string)
    if not r:
        return string
    # print(r.group())
    index = r.span()
    expr = r.group()
    string = string[:index[0]] + expr[1:-1] + string[index[1]:]
    # print(string)
    return string


def split_expr(string):
    '''
    把字符串表达式转换为列表
    '''
    length = len(string)
    count = 0
    op = ''
    number = ''
    expr_list = []

    while count < length:
        char = string[count]
        if char.isdigit() or char == '.':
            number += char
        elif char in ['+', '-', '*', '/']:
            op = char
            if number:
                expr_list.append(number)
                expr_list.append(op)
                number = ''
                op = ''
            else:
                number += char
        elif char == 'e':
            number += 'e-'
            count += 2
            continue
        count += 1
    if number:
        expr_list.append(number)
    # print(expr_list)
    return expr_list


def gather1(expr_list):
    '''
    计算表达式列表里的乘除
    '''
    length = len(expr_list)
    count = 0
    number1 = ''
    number2 = ''
    op = ''
    while count < length:
        elem = expr_list[count]
        if elem in ['*', '/']:
            op = elem
            number1 = expr_list[count-1]
            number2 = expr_list[count+1]
            if op == '*':
                result = float(number1) * float(number2)
            else:
                result = float(number1) / float(number2)
            expr_list[count] = str(result)
            expr_list[count-1] = expr_list[count+1] = ''
            break
        count += 1

    while '' in expr_list:
        expr_list.remove('')

    return expr_list


def gather2(expr_list):
    '''
    计算表达式列表里的加减
    '''
    length = len(expr_list)
    count = 0
    number1 = ''
    number2 = ''
    op = ''
    while count < length:
        elem = expr_list[count]
        if elem in ['+', '-']:
            op = elem
            number1 = expr_list[count-1]
            number2 = expr_list[count+1]
            if op == '+':
                result = float(number1) + float(number2)
            else:
                result = float(number1) - float(number2)
            expr_list[count] = str(result)
            expr_list[count-1] = expr_list[count+1] = ''
            break
        count += 1

    while '' in expr_list:
        expr_list.remove('')

    return expr_list


def handler2(string):
    '''
    提取括号内的表达式
    '''
    r = re.findall(r'\([-+*/.e0-9]+\)', string)
    if not r:
        return string    
    # print(r)
    for expr_str in r:
        expr_str1 = expr_str[1:-1]
        expr_list = split_expr(expr_str1)
        # print(expr_list)

        while True:
            if '*' in expr_list or '/' in expr_list:
                expr_list = gather1(expr_list)
            else:
                break

        while True:
            if '+' in expr_list or '-' in expr_list:
                expr_list = gather2(expr_list)
            else:
                break

        # print(expr_list)
        string = string.replace(expr_str, expr_list[0], 1)
        # print(string)

    return string


def handler3(string):
    '''
    计算最后的表达式
    '''
    expr_list = split_expr(string)
    # print(expr_list)

    while True:
        if '*' in expr_list or '/' in expr_list:
            expr_list = gather1(expr_list)
        else:
            break

    while True:
        if '+' in expr_list or '-' in expr_list:
            expr_list = gather2(expr_list)
        else:
            break

    # print(expr_list)
    string = expr_list[0]
    # print(string)

    return string


def main(string):
    '''
    计算入口
    '''
    string = string.replace(' ', '')
    print(string)
    
    while True:
        new_string = handler1(string)
        if new_string == string:
            break
        else:
            string = new_string

    while True:
        new_string = handler2(string)
        if new_string == string:
            break
        else:
            string = new_string

    string = handler3(string)
    return string


# 开始计算
a = '1-2*((60 - 30+(-40/5)*( 9-2*5/3+7/3*99/4*2998+10*568/14))-(-4*3)/(16-3*2))'
result_a = 2776672.6952380957

b = '1-2*((60 - 30+(-40/5)*( 9-21*5.343e-7/3+7/3*99/(-4.4353e-7)*2998+10*568/(-14)))-(-4*3)/(16-3*2)+(-40/5))'
result_b = -24982770055769.426


print(main(b))
# print(main(a))